package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.common.NonNullUtil;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.utils.util.StringUtils;
import org.springframework.util.CollectionUtils;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 排水管网服务
 * 
 * 2017/5/17 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
public class SurfaceService {
	public final static SurfaceService INSTANCE = new SurfaceService();

	public List<RowBase> getStationList(String groupCode) throws Exception {
		List<RowBase> rows;
		if (!StringUtils.IsNullOrSpace(groupCode)) {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			parms.add(new GlobalParameter("groupCode", groupCode));
			String sql = "select t.name \"name\", t.code \"id\", '0' as \"count\"\n"
					+ "  from BC_SITE_INFO t\n"
					+ "  left join OPERATION_GROUP_STATION_ASSO t1\n"
					+ "    on t.exp_no = t1.station_id\n"
					+ " where t.station_category = '1'\n"
					+ "   and t.BUILD_STATUS = '0'\n"
					+ "   and t.operation_unit = '深圳市浩瑞泰科技有限公司'\n"
					+ "   and t1.operation_group_id = @groupCode";
			rows = DataUtil.getSelectRows(sql, parms);
		} else {
			String sql = "select distinct t.name \"name\", t.code \"id\", '0' as \"count\"\n"
					+ "  from BC_SITE_INFO t\n"
					+ "  left join OPERATION_GROUP_STATION_ASSO t1\n"
					+ "    on t.exp_no = t1.station_id\n"
					+ " where t.station_category = '1'\n"
					+ "   and t.BUILD_STATUS = '0'\n"
					+ "   and t.operation_unit = '深圳市浩瑞泰科技有限公司'\n";
			rows = DataUtil.getSelectRows(sql, null);
		}
		return NonNullUtil.toList(rows);
	}

	public Map<String, String> getTaskCountMap(String stationCode) throws Exception {
		ValidUtils.hasText(stationCode, "站点编号为空，任务数获取失败");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationCode", stationCode));
		String sql = "select count(job_id) count, t.job_id\n"
				+ "  from OP_TASK t\n"
				+ " where t.TASK_STATUS = '0'\n"
				+ "   and t.station_id = @stationCode\n"
				+ " group by job_id";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		Map<String, String>resultMap = new HashMap<>();
		if (!CollectionUtils.isEmpty(rows)) {
			for (RowBase rowBase : rows) {
				String jobId = rowBase.getSafeString("job_id");
				String count = rowBase.getSafeString("count");
				resultMap.put(jobId, count);
			}			
		}		
		return resultMap;
	}

}
